package com.zhkj.shixi.tvfilemanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhkj.shixi.tvfilemanager.FileManager;
import com.zhkj.shixi.tvfilemanager.R;
import com.zhkj.shixi.tvfilemanager.ViewEntity;
import com.zhkj.shixi.tvfilemanager.view.GrideViewPager;

import java.util.ArrayList;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;

/**
 * Created by nieshuang on 19-8-12.
 */

public class ListViewAdapter extends BaseAdapter{
    private boolean isGridVirew = true;
    private LayoutInflater mInflater = null;
    private Context mContext;
    private ArrayList<ViewEntity> mArrayList = null;
    public ListViewAdapter(Context context, ArrayList<ViewEntity> arrayList){
        this.mContext = context;
        this.mArrayList = arrayList;
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public void setmArrayList(ArrayList<ViewEntity> mArrayList) {
        this.mArrayList = mArrayList;
    }

    @Override
    public int getCount() {
        return mArrayList == null ? 0 : mArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return mArrayList == null ? null : mArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ViewEntity mBean = (ViewEntity) getItem(position);
        
        if (convertView == null) {
            convertView = mInflater.from(mContext).inflate(R.layout.file_list_item, parent, false);
            holder = new ViewHolder();

            holder.imageView = (ImageView) convertView.findViewById(R.id.list_useImage);
            holder.textViewName = (TextView) convertView.findViewById(R.id.list_file_name);
            holder.textViewFileSize = (TextView) convertView.findViewById(R.id.list_file_size);
            holder.textViewFileTime = (TextView) convertView.findViewById(R.id.list_file_time);
            holder.selectId = (ImageView) convertView.findViewById(R.id.listSelected);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (fileManager.getType() == FileManager.IMAGE_TYPE && mBean.getmFile().isFile()){
            holder.imageView.setImageBitmap(FileManager.getImageThumbnail(mBean.getmFile().getAbsolutePath(),FileManager.getScreenWidth(mContext)/13,FileManager.getScreenHeight(mContext)/13));
        }else {
            holder.imageView.setImageResource(mBean.getmImageId());
        }
        holder.textViewName.setText(mBean.getmFileName());
        holder.textViewFileSize.setText(mBean.getmFileSize());
        holder.textViewFileTime.setText(mBean.getmTime());

        if(GrideViewPager.bMap.get(position) == null )
        {
            holder.selectId.setVisibility(View.INVISIBLE);
        }
        else if(GrideViewPager.bMap.get(position) .equals( View.INVISIBLE))
            holder.selectId.setVisibility(View.INVISIBLE);
        else if(GrideViewPager.bMap.get(position) .equals( View.VISIBLE))
            holder.selectId.setVisibility(View.VISIBLE);
        return convertView;

    }

    class ViewHolder {
        ImageView imageView;
        TextView textViewName;
        TextView textViewFileSize;
        TextView textViewFileTime;
        ImageView selectId;

    }

}
